cc.Class({
    extends: cc.Component,
    properties: {
        type: window.GameEnum.TokenTypeEnum.TOKEN_ADVANCE,
        value: "",
    },

    onLoad: function () {
    },
    onEnable: function () {
        this.init();
    },
    init: function () {
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndFun, this);
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartFun, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveFun, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancelFun, this);

    },
    update: function (dt) {
    },
    isSysBtn: function () {
        return window.GameViewData.sysTokenBtnArr.indexOf(this) > -1;
    },
    changeAppearance: function () {
        this.node.opacity = window.GameConstant.GameDefaultValue.OPACITY_MID;
    },
    resetAppearance: function () {
        this.node.opacity = window.GameConstant.GameDefaultValue.OPACITY_MAX;
    },

    /**
     * 触摸结束，包括end和cancel
     * !#en 
     * !#zh 触摸结束，包括end和cancel
     */
    moveDown: function () {
        // 触摸结束，包括end和cancel
        this.resetAppearance();
        window.StaticFunc.destroy(window.GameViewData.movingNode);
    },
    touchStartFun() {
        window.GameViewData.movingNode = cc.instantiate(this.node);
        this.changeAppearance();
        // 将movingNode的父节点设为gameView
        // 下面是进行坐标转换
        var vecByThisInTheWorld = this.node.parent.convertToWorldSpaceAR(window.GameViewData.movingNode.getPosition());
        var newVec2 = window.GameViewData.gameView.node.convertToNodeSpaceAR(vecByThisInTheWorld);
        window.GameViewData.movingNode.x = newVec2.x;
        window.GameViewData.movingNode.y = newVec2.y;
        window.GameViewData.movingNode.parent = window.GameViewData.gameView.node;
    },
    touchMoveFun(event) {
        console.log("touchMoveFun");
        let delta = event.touch.getDelta();
        window.GameViewData.movingNode.x += delta.x;
        window.GameViewData.movingNode.y += delta.y;
    },
    touchEndFun() {
        this.moveDown();
        if (this.isSysBtn()) {
            window.TokenManager.addToken(window.GameConstant.TouchLayout.LAYOUT_MAIN, this, -1);
        }
    },
    changeIntersectionLayoutToken(touchType, layout, arr) {
        // 判断移动过来的组件，是否到了layout的范围里面
        var intersectionRect = window.StaticFunc.getIntersectionBetweenTwoNodeOnTheWorld(window.GameViewData.movingNode, layout.node);

        if (intersectionRect.width > 0 && intersectionRect.height > 0) {
            var maxArea = 0;
            var maxIndex = -1;
            var maxRect = null;
            var changeCount = 0;

            // 遍历item和组件的相交关系
            for (var tokenItemIndex = 0; tokenItemIndex < arr.length; tokenItemIndex++) {
                if (changeCount > 3) {
                    break;
                }

                var tokenItem = arr[tokenItemIndex];
                var tmpIntersectionRectRect = window.StaticFunc.getIntersectionBetweenNodeRect(tokenItem.node, intersectionRect);

                // 获取相交区域最大的item
                if (tmpIntersectionRectRect.width > 0 && tmpIntersectionRectRect.height > 0) {
                    var area = tmpIntersectionRectRect.width * tmpIntersectionRectRect.height;
                    if (maxArea < area) {
                        maxArea = area;
                        maxIndex = tokenItemIndex;
                        maxRect = tmpIntersectionRectRect;
                        changeCount++;
                    }
                }
            }

            // 几个判断条件
            // 没有和任何一个item触碰
            // 和某个item触碰范围大于0.65
            // 拖动的组件在触碰item的右边
            // 拖动的组件在触碰item的左边 及其他情况
            var resultIndex = -1;
            if (maxIndex < 0) {
                resultIndex = -1;
            } else if (maxArea > intersectionRect.width * intersectionRect.height * 0.65) {
                window.TokenManager.removeToken(touchType, maxIndex);
                resultIndex = maxIndex;
            } else if (maxRect.x == intersectionRect.x) {
                // 移拖动的组件 在 触碰item的右边
                resultIndex = maxIndex + 1;
            } else {
                resultIndex = maxIndex;
            }

            window.TokenManager.addToken(touchType, this, resultIndex);
        }

        if (!this.isSysBtn()) {
            for (var i = 0; i < arr.length; i++) {
                arr[i].node == this.node && window.TokenManager.removeToken(touchType, i);
            }
        }
    },
    touchCancelFun() {
        this.moveDown();
        this.changeIntersectionLayoutToken(window.GameConstant.TouchLayout.LAYOUT_MAIN, window.GameViewData.tokenLayout, window.GameViewData.dynamicData.tokenArr);
        this.changeIntersectionLayoutToken(window.GameConstant.TouchLayout.LAYOUT_FUNC1, window.GameViewData.func1Layout, window.GameViewData.dynamicData.func1TokenArr);
        this.changeIntersectionLayoutToken(window.GameConstant.TouchLayout.LAYOUT_FUNC2, window.GameViewData.func2Layout, window.GameViewData.dynamicData.func2TokenArr);
    },
});

